package com.mt.mtyx.activity.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mt.mtyx.activity.service.CouponInfoService;
import com.mt.mtyx.common.exception.BusinessException;
import com.mt.mtyx.common.result.Result;
import com.mt.mtyx.dto.activity.CouponRuleDto;
import com.mt.mtyx.entity.activity.CouponInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

/**
 * 优惠券控制层
 *
 * @author WMT
 * @Date 2023/7/24 15:55
 */
@Api(tags = "优惠券")
@RequestMapping("/admin/activity/couponInfo")
@RestController
public class CouponInfoController {

    @Autowired
    private CouponInfoService couponInfoService;

    @ApiOperation("分页条件查询优惠券")
    @GetMapping("/{page}/{limit}")
    public Result<IPage<CouponInfo>> selectAll(
            @PathVariable Long page,
            @PathVariable Long limit
    ) {
        return Result.success(couponInfoService.selectAll(new Page<CouponInfo>(page, limit)));
    }

    @ApiOperation("查询优惠券详情")
    @GetMapping("/get/{id}")
    public Result<CouponInfo> getDetails(@PathVariable Long id) {
        if (StringUtils.isEmpty(id)) {
            throw new BusinessException("id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        CouponInfo couponInfo = couponInfoService.getById(id);
        if (couponInfo != null) {
            couponInfo.setCouponTypeString(couponInfo.getCouponType().getComment());
            if (couponInfo.getRangeType() != null) {
                couponInfo.setRangeTypeString(couponInfo.getRangeType().getComment());
            }
        }
        return Result.success(couponInfo);
    }

    @ApiOperation("新增优惠券")
    @PostMapping("/save")
    public Result<Integer> save(@RequestBody CouponInfo couponInfo) {
        if (StringUtils.isEmpty(couponInfo.getCouponName())) {
            throw new BusinessException("优惠券名称不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        if (couponInfoService.existsByName(couponInfo.getCouponName())) {
            throw new BusinessException("优惠券名称已存在！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(couponInfoService.insert(couponInfo));
    }

    @ApiOperation("删除优惠券")
    @DeleteMapping("/remove/{id}")
    public Result<Integer> remove(@PathVariable Long id) {
        if (StringUtils.isEmpty(id)) {
            throw new BusinessException("id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(couponInfoService.delete(id));
    }

    @ApiOperation("删除优惠券")
    @DeleteMapping("/batchRemove")
    public Result<Integer> remove(@RequestBody List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            throw new BusinessException("id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(couponInfoService.batchDelete(ids));
    }

    @ApiOperation("修改优惠券")
    @PostMapping("/update")
    public Result<Integer> update(@RequestBody CouponInfo couponInfo) {
        if (StringUtils.isEmpty(couponInfo.getId())) {
            throw new BusinessException("id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        if (StringUtils.isEmpty(couponInfo.getCouponName())) {
            throw new BusinessException("优惠券名称不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        CouponInfo couponInfoDb = couponInfoService.getById(couponInfo.getId());
        if (!couponInfoDb.getCouponName().equals(couponInfo.getCouponName())) {
            if (couponInfoService.existsByName(couponInfo.getCouponName())) {
                throw new BusinessException("优惠券名称已存在！", HttpStatus.BAD_REQUEST.value());
            }
        }
        return Result.success(couponInfoService.update(couponInfo));
    }

    @ApiOperation("根据优惠券id获取规则数据")
    @GetMapping("/findCouponRuleList/{couponId}")
    public Result<Map<String, Object>> getCouponRuleList(@PathVariable Long couponId) {
        if (StringUtils.isEmpty(couponId)) {
            throw new BusinessException("优惠券id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        // 1.查询优惠券信息
        CouponInfo couponInfo = couponInfoService.getById(couponId);
        if (couponInfo == null) {
            throw new BusinessException("优惠券不存在！",HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(couponInfoService.getCouponRuleList(couponId, couponInfo));
    }

    /* ================================= 优惠券规则相关接口 ================================= */

    @ApiOperation("添加优惠券规则")
    @PostMapping("/saveCouponRule")
    public Result<Integer> saveCouponRule(@RequestBody CouponRuleDto dto) {
        if (StringUtils.isEmpty(dto.getCouponId())) {
            throw new BusinessException("优惠券id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(couponInfoService.saveCouponRule(dto));
    }

    @ApiOperation("根据关键字获取sku列表，活动使用")
    @GetMapping("/findCouponByKeyword/{keyword}")
    public Result<List<CouponInfo>> findCouponByKeyword(@PathVariable String keyword) {
        if (StringUtils.isEmpty(keyword)) {
            throw new BusinessException("请输入正确的关键词！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(couponInfoService.findCouponByKeyword(keyword));
    }
}
